import Header from '@/components/Header.vue';
import Footer from '@/components/Footer.vue';
import vueSeamlessScroll from 'vue-seamless-scroll';
import { getSearchParams, loadViewData } from '@/utils';
import mitts from '@/utils/mitt';
import { v4 as uuid } from 'uuid';
import { getWaitePatientData, getCalledPatientData } from '@/api/screens/pharmacy';
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  components: {
    Header,
    Footer,
    vueSeamlessScroll
  },
  data() {
    return {
      deviceInfo: {},
      callList: [],
      waitList: [],
      rollKey: uuid(),
      jsonData: {
        region_img_201_1: '',
        region_text_100_1: {},
        region_text_100_2: {}
      }
    };
  },
  computed: {
    classOption() {
      return {
        autoPlay: true,
        step: 1, // 数值越大速度滚动越快
        limitMoveNum: 18, // 开始无缝滚动的数据量 this.dataList.length
        direction: 1 // 0向下 1向上 2向左 3向右
      };
    }
  },
  mounted() {
    const programId = getSearchParams('instance_id');
    this.loadView(programId);
    // 获取设备基本信息
    this.$AndroidSdk.deviceInfoAction(JSON.stringify({ action: 'PharmacyDeviceInfoAction', callback: 'getDeviceInfoCallBack' }));
    mitts.on('deviceInfoPing', this.deviceInfoPing);

    // 药房窗口屏叫号MQ回调
    if (window) {
      window.call_update = this.dealMQData; // 处理叫号MQ
    }
  },
  methods: {
    loadView(programId) {
      programId = programId || '';
      if (programId === '') return;
      loadViewData(programId).then((data) => {
        this.jsonData = { ...data };
      });
    },
    deviceInfoPing(info) {
      this.deviceInfo = info;
      this.$AndroidSdk.writeLog('收到设备基础信息---->' + JSON.stringify(info));
      // 等候患者
      this.getWaitePatientDatas();
      // 已呼叫患者
      this.getCalledPatientDatas();
    },
    getWaitePatientDatas() {
      let params = {
        screenId: this.deviceInfo.screenId,
        hospitalId: this.deviceInfo.hospitalId
      };
      // 等候患者
      getWaitePatientData(params).then((res) => {
        if (res.status === 0) {
          let arr = res.data || [];
          let rs = [];
          arr.map((item) => {
            rs = [...rs, ...item.patients];
          });
          this.waitList = rs;
        }
      });
    },
    getCalledPatientDatas() {
      let params = {
        screenId: this.deviceInfo.screenId,
        hospitalId: this.deviceInfo.hospitalId
      };
      // 已呼叫患者
      getCalledPatientData(params).then((res) => {
        if (res.status === 0) {
          let arr = res.data || [];
          let rs = [];
          arr.map((item) => {
            rs = [...rs, ...item.patients];
          });
          this.callList = rs.reverse();
        }
      });
    },
    dealMQData(obj) {
      this.$AndroidSdk.writeLog('收到MQ---->' + JSON.stringify(obj));
      try {
        switch (obj.msgId) {
          case 'Pharmacy_Window_Wait_Refresh':
            // 刷新等候患者
            this.getWaitePatientDatas();
            break;
          case 'Pharmacy_Window_Called_Refresh':
            // 刷新已呼叫患者
            this.getCalledPatientDatas();
            break;
          case 'Pharmacy_Window_Calling_Patient':
            this.getCalledPatientDatas();
            break;
          default:
            this.getWaitePatientDatas();
            this.getCalledPatientDatas();
            break;
        }
      } catch (e) {
        this.$AndroidSdk.writeLog('dealMQData---->' + e.message);
      }
    },
    handleScrollEnd() {
      this.rollKey = uuid();
    }
  }
};
